package com.example.mine_platform.service.impl;

import com.example.mine_platform.commom.Result;
import com.example.mine_platform.mapper.ProductionMapper;
import com.example.mine_platform.mapper.UserMapper;
import com.example.mine_platform.mapper.ProductionUserMapper;
import com.example.mine_platform.pojo.dto.ProductionDTO;
import com.example.mine_platform.pojo.dto.ProductionNewDTO;
import com.example.mine_platform.pojo.entities.Production;
import com.example.mine_platform.pojo.entities.ProductionUser;
import com.example.mine_platform.pojo.vo.ProductionDetailVO;
import com.example.mine_platform.pojo.vo.ProductionVO;
import com.example.mine_platform.service.ProductionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductionServiceImpl implements ProductionService {
    @Resource
    private ProductionMapper productionMapper;
    @Resource
    private ProductionUserMapper productionUserMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<ProductionVO> getAllProduction() {
        List<Production> productionList = productionMapper.getAllProduction();
        List<ProductionVO> productionVOList = new ArrayList<>();
        for (Production x : productionList) {
            ProductionVO productionVO = new ProductionVO();
            productionVO.setId(x.getId());
            productionVO.setName(x.getName());
            productionVO.setStart_time(x.getStartDate());
            productionVO.setEnd_time(x.getEndDate());
            productionVO.setOutput(x.getOutput());
            productionVO.setManager(userMapper.selectByPrimaryKey(x.getManager()).getUserName());
            productionVO.setActual_output(x.getActualOutput());
            productionVO.setActual_date(x.getActualDate());
            productionVO.setStatus(x.getStatus());
            productionVOList.add(productionVO);
        }
        return productionVOList;
    }

    @Override
    public ProductionDetailVO getProductionDetail(Integer id) {
        Production production = productionMapper.selectByPrimaryKey(id);
        List<Integer> participants = new ArrayList<>();
        ProductionDetailVO productionDetailVO = new ProductionDetailVO();
        productionDetailVO.setId(production.getId());
        productionDetailVO.setName(production.getName());
        productionDetailVO.setDescribe(production.getDescribe());
        productionDetailVO.setStart_date(production.getStartDate());
        productionDetailVO.setEnd_date(production.getEndDate());
        productionDetailVO.setOutput(production.getOutput());
        productionDetailVO.setManager(production.getManager());
        productionDetailVO.setActual_output(production.getActualOutput());
        productionDetailVO.setActual_date(production.getActualDate());
        productionDetailVO.setStatus(production.getStatus());

        List<ProductionUser> productionUserList = productionUserMapper.getAllUsersById(production.getId());
        for (ProductionUser x : productionUserList) {
            participants.add(x.getUserId());
        }
        productionDetailVO.setParticipants(participants);
        return productionDetailVO;
    }

    @Override
    public Result insertNewProduction(ProductionNewDTO productionNewDTO) {
        List<Integer> participants = productionNewDTO.getParticipants();
        Production production = new Production();
        production.setName(productionNewDTO.getName());
        production.setDescribe(productionNewDTO.getDescribe());
        production.setStartDate(productionNewDTO.getStart_date());
        production.setEndDate(productionNewDTO.getEnd_date());
        production.setOutput(productionNewDTO.getOutput());
        production.setManager(productionNewDTO.getManager());

        productionMapper.insertSelective(production);
        Integer productionId = production.getId();
        for (Integer userId : participants) {
            ProductionUser productionUser = new ProductionUser();
            productionUser.setProductionId(productionId);
            productionUser.setUserId(userId);
            productionUserMapper.insertSelective(productionUser);
        }
        return Result.success();
    }

    @Override
    public Result updateProduction(ProductionDTO productionDTO) {
        Integer productionId = productionDTO.getId();
        List<Integer> participantsId = productionDTO.getParticipants();  //修改后的id
        List<ProductionUser> productionUserList = productionUserMapper.getAllUsersById(productionId);  //目前存储的id
        Production production = productionMapper.selectByPrimaryKey(productionId);
        production.setName(productionDTO.getName());
        production.setDescribe(productionDTO.getDescribe());
        production.setManager(productionDTO.getManager());
        production.setActualOutput(productionDTO.getActual_output());
        production.setActualDate(productionDTO.getActual_date());
        production.setStatus(productionDTO.getStatus());
        productionMapper.updateByPrimaryKey(production);
        for (ProductionUser x : productionUserList) {
            x.setDeleted((byte) 1);
            productionUserMapper.updateByPrimaryKey(x);
        }
        for (Integer userId : participantsId) {
            ProductionUser productionUser = new ProductionUser();
            productionUser.setProductionId(productionId);
            productionUser.setUserId(userId);
            productionUserMapper.insertSelective(productionUser);
        }
        return Result.success();
    }

    @Override
    public Result deleteProduction(Integer id) {
        Production production = productionMapper.selectByPrimaryKey(id);
        production.setDeleted((byte) 1);
        productionMapper.updateByPrimaryKey(production);
        List<ProductionUser> productionUserList = productionUserMapper.getAllUsersById(id);
        for (ProductionUser x : productionUserList) {
            x.setDeleted((byte) 1);
            productionUserMapper.updateByPrimaryKey(x);
        }
        return Result.success();
    }
}
